package com.autonavi.abs.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Random;

/**
 * @author wwy
 * @version V1.0
 * <p>Description: 订单号生成器</p>
 * @date 2020-07-23 14:09
 */
public class OrderIdUtils {

    private static Random randomGenerator = new Random();

    /**
     * yyMMddHHmmssSSS+随机数
     *
     * @return
     */
    public static String newOrderID() {
        int rand = 0;
        synchronized (randomGenerator) {
            rand = randomGenerator.nextInt(100000);
            String s = StringUtils.join(DateTimeUtils.formatNow("yyMMddHHmmssSSS"),
                    StringUtils.leftPad(String.valueOf(rand), 5, '0'));
            return s;
        }
    }

    /**
     * timestamp + 随机数
     *
     * @return
     */
    public static String newOrderID2() {
        int rand = 0;
        synchronized (randomGenerator) {
            rand = randomGenerator.nextInt(100000);
            String s = StringUtils.join("" + System.currentTimeMillis(),
                    StringUtils.leftPad(String.valueOf(rand), 5, '0'));
            return s;
        }
    }

}
